package meterMonitor.controller;

import meterMonitor.biz.GatewayBiz;
import meterMonitor.biz.ReaderBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.model.Gateway;
import meterMonitor.model.Reader;
import meterMonitor.vo.BaseResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.List;

@RequestMapping("sensorManage")
@RestController
public class SensorManageController {
    public static final Logger logger = LoggerFactory.getLogger(SensorManageController.class);

    @Autowired
    ReaderBiz readerBiz;

    @Autowired
    GatewayBiz gatewayBiz;

    /**
     * 传感器管理页面
     *
     * @param mv
     * @return
     */
    @RequestMapping("sensorManage.do")
    public ModelAndView index(ModelAndView mv) {
        return mv;
    }

    /**
     * @return 所有读表器
     */
    @RequestMapping("getAll.do")
    public List<Reader> getAll() {
        return readerBiz.query(new Reader());
    }


    /**
     * 删除某个读表器
     *
     * @param reader
     */
    @RequestMapping("delete.do")
    public BaseResponseData delete(Reader reader) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            readerBiz.delete(reader);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("删除成功！");
        } catch (Exception e) {
            logger.error("删除网关信息异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("删除失败！");
        }
        return baseResponseData;
    }

    /**
     * 修改读表器
     *
     * @param reader
     */
    @RequestMapping("update.do")
    public BaseResponseData update(Reader reader) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            readerBiz.update(reader);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("修改成功！");
        } catch (Exception e) {
            logger.error("修改异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("修改失败！");
        }
        return baseResponseData;
    }

    /**
     * 新增读表器
     *
     * @return
     */
    @RequestMapping("add.do")
    @FireAuthority(authorityTypes = AuthorityType.Edit)
    @ResponseBody
    public BaseResponseData add(Reader reader) {

        System.out.println(reader.getReaderCode()+"----");
        System.out.println(reader.getIp()+"----");

        Gateway gateway = gatewayBiz.queryOneByIp(reader.getIp());
        reader.setGatewayId(gateway.getGatewayId());


        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            reader.setAddedTime(new Timestamp(System.currentTimeMillis()));
            readerBiz.add(reader);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("添加成功！");
        } catch (Exception e) {
            logger.error("新增异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("新增失败！");
        }
        return baseResponseData;
    }
}
